#!/bin/bash
#Author: YangYi
#UpDate: 2024-04-08
#Version: v1
#Content: 节点容器句柄数统计接入普米看板


#UpDate: 2024-04-11
#Version: v2
#Content: 增加ip对应集群名判断

#脚本运行时间统计
startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`


#设置要保存的文件个数
SAVE_COUNT="720"

#格式化时间
TIME=`date +%Y:%m:%d:%H:%M:%S`

#容器列表
DOCKERLIST=`docker ps | grep -v "pause\|filebeat\|traefik" | awk '{print $1}' | grep -v CONTAINER`

#存储路径
FILE_PATH=/app/paasadmin/ContainerOpenFileResult/$TIME-ContainerResult.txt

#$临时文件
TEMPORARY_FILE=/app/paasadmin/ContainerList.txt

#文件过滤路径
FILTER_FILE=/app/paasadmin/ContainerOpenFileResult/
/bin/mkdir -p  $FILTER_FILE && chown -R paasadmin:paasadmin $FILTER_FILE

#普米采集文件
PROMETHEUS_FILE="/home/patrol/prometheus_containers_apps_openfile.prom"

#普米指标格式化
TITLE="
# HELP prometheus_containers_apps_openfile_total Total number of WAL corruptions.\n# TYPE prometheus_container_app_openfile_total gauge
"

# 获取网卡ip
network=`/sbin/ip a s  | grep -iw "eth0\|bond0" | grep -i "10.145." | awk '{print $2}' | awk -F "/" '{print $1}' | head -1`
  
#初始化集群信息
network_type=""

#Ip对应集群名判断
function MatchCluster(){
# 使用 case 语句和条件表达式来判断 IP 地址区间  
case $network in  
    # 第一个区间 
    10.145.167.*)  
        network_type="paas-crm-container-c2"  
        ;;  
    # 第二个区间 
    10.145.169.[3-8])  
        network_type="paas-crm-container-c3"  
        ;;  
    # 第三个区间 
    10.145.158.7[7-9])  
        network_type="paas-crm-container-c4"  
        ;;  
    10.145.158.8[0-2])  
        network_type="paas-crm-container-c4"  
        ;;  
    10.145.158.13[7-9])  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.158.14[0-9])  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.158.15[0-4])  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.158.[8-9])  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.158.1[0-3])  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.158.18[7-9])  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.158.190)  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.158.211)  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.158.169)  
        network_type="paas-crm-container-c5"  
        ;;  
    10.145.151.*)  
        network_type="acct-ccse-c1"  
        ;;  
    10.145.149.*)  
        network_type="mss-ccse-001"  
        ;;  
    10.145.166.1[0-7])  
        network_type="oss-ccse-01"  
        ;;  
    10.145.166.2[6-9])  
        network_type="oss-ccse-02"  
        ;;  
    10.145.166.3[0-6])  
        network_type="oss-ccse-02"  
        ;;  
    10.145.166.4[2-9])  
        network_type="oss-ccse-03"  
        ;;  
    10.145.166.5[0-7])  
        network_type="oss-ccse-03"  
        ;;  
    10.145.166.8[5-9])  
        network_type="oss-ccse-04"  
        ;;  
    10.145.166.9[0-2])  
        network_type="oss-ccse-04"  
        ;;  
    10.145.166.6[5-9])  
        network_type="public-ccse-c1"  
        ;;  
    10.145.166.7[0-6])  
        network_type="public-ccse-c1"  
        ;;  
    10.145.175.*)  
        network_type="paas-ccse-ipsdn"  
        ;;  
    # 如果 IP 不在任何区间内  
    *)  
        network_type="Unknown Network"  
        ;;  
  esac  
}


#句柄数服务统计
function ContainerFilter(){
  if [  -f $TEMPORARY_FILE ];then
    rm -f $TEMPORARY_FILE
  fi
  for v in ${DOCKERLIST[@]}
    do
      openFile=`docker container top $v | awk 'NR == 1 {next}{print$2}'| xargs -i ls -l /proc/{}/fd | wc -l`
      podName=`docker inspect $v | grep -iw "io.kubernetes.pod.name" | awk '{print $2}' | awk -F "\"" '{print $2}'`
      NameSpace=`docker inspect $v | grep -iw "io.kubernetes.pod.namespace" | awk '{print $2}' | awk -F "\"" '{print $2}'`
      echo $openFile" "$NameSpace" "$podName" "$network" "$network_type >> $TEMPORARY_FILE
    done
  #排序
  sudo cat $TEMPORARY_FILE | sort  -nr >> $FILE_PATH
  
  #过滤文件  
  AlterFile=`ls -l $FILTER_FILE  | awk '{print $9}' | tail -1`  

  #指标抬头
  echo -e $TITLE > $PROMETHEUS_FILE
  
  #普米采集格式  
  cat $FILTER_FILE$AlterFile | awk '{print "prometheus_containers_apps_openfile_total{namespace=\""$2"\",pods=\""$3"\",nodeIp=\""$4"\",clusterName=\""$5"\",real_port=\""$3"\"} "$1 }' >> $PROMETHEUS_FILE

  #文本格式转换
  dos2unix $PROMETHEUS_FILE > /dev/null 2>&1
  
}


#文件清理
function File_Cleanup(){
cd $FILTER_FILE
#1 判断文件个数
count=$(ls -l | wc -l)
delcount=$[$count - $SAVE_COUNT]
#2 是否进行删除
if [ $delcount -gt 0 ] ; then
        #获取时间最久的文件夹名称
        filesname=$(ls -tr | head -n $delcount)
        filesname=${filesname// /}
        filesarr=($filesname)
        #循环依次删除时间久的文件
        for each in ${filesarr[*]}
          do
          sudo rm -rf $each
          done
fi
}

MatchCluster
ContainerFilter
File_Cleanup

echo "所属集群是: ============>  "$network_type

#色卡
GREEN='\033[0;32m'
NO_COLOR='\033[0m' # 用于重置颜色
HIGHLIGHT='\033[1m'
NO_HIGHLIGHT='\033[22m'

#时间统计
endTime=`date +%Y%m%d-%H:%M:%S`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]
echo -e "${HIGHLIGHT}${GREEN}脚本运行成功,输出结果请查看普米采集文件: /home/patrol/prometheus_containers_apps_openfile.prom ${NO_HIGHLIGHT}${NO_COLOR}"
echo "启始时间:$startTime" 
echo "结束时间:$endTime"  
echo "脚本运行耗时: $sumTime seconds"







